<?php
include_once 'class.mydict.php';
if (isset($_REQUEST['key'])) {
	$key = $_REQUEST['key'];
	if (isset($_POST['update'])) {
		$cats = $_POST['cats'];
		MyDict::updateMeaningCats($key, $cats);
	}
	$cats = MyDict::getMeaningCatTree($key);
	$cat_dynatree = array();
	push_dynatree_node_recur($cat_dynatree, $cats);
	$row = MyDict::queryKey($key);
	?>
	<form name="word_cats" method="post">
		<fieldset>
		<legend>Current word</legend>
		key: <?php echo $row['key']?> | meaning: <?php echo $row['meaning']?><br />
		<div id="word_cats"></div>
		<input type="hidden" name="cats" value="" />
		<input type="submit" value="Update" name="update" onClick="get_selected_cats()" />
		</fieldset>
	</form>
	<?php
} else {
	if (isset($_POST['add'])) {
		$name = $_POST['cat_name'];
		$parent_id = $_POST['cat_parent'];
		MyDict::addMeaningCat($name, $parent_id);
	}
	$cats = MyDict::getMeaningCatTree();
	$cat_dynatree = array();
	push_dynatree_node_recur($cat_dynatree, $cats);
?>
	<form name="cats" method="post">
		<select name="cat_parent">
			<?php cats_2_options_recur($cats, '')?>
		</select>
		<input type="text" name="cat_name" value="" />
		<input type="submit" value="Add" name="add" />
	</form>
<?php }?>
<div id="cats"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src='js/jquery/jquery.cookie.js' type="text/javascript"></script>
<script src='js/dynatree-1.2.4/jquery.dynatree.js' type="text/javascript"></script>
<?php if (isset($_REQUEST['key'])) : //Manage word cat?>
<script type="text/javascript">
    $(function(){
        $("#word_cats").dynatree({
        	/*onClick: function(node, event) {
				if( ! node.data.isFolder )
					node.toggleSelect();
			},*/
            imagePath: "skin-custom/",
            clickFolderMode: 1,
            persist: false,
            //activeVisible: true,
            cookieId: "word_cats_<?php echo $key?>",
            checkbox: true,
            selectMode: 2,
            children: <?php echo json_encode($cat_dynatree)?>
        });
    });
    function get_selected_cats() {
		var selNodes = $("#word_cats").dynatree("getTree").getSelectedNodes();
		var selKeys = $.map(selNodes, function(node){
			   return node.data.key;
		});
		var cats = selKeys.join(",");
		$('form[name="word_cats"] input[name="cats"]').val(cats);
    }
</script>
<?php else : //Manage cat?>
<script type="text/javascript">
    $(function(){
        $("#cats").dynatree({
        	onClick: function(node, event) {
			},
            imagePath: "skin-custom/",
            clickFolderMode: 1,
            persist: true,
            cookieId: "cats",
            checkbox: false,
            selectMode: 1,
            children: <?php echo json_encode($cat_dynatree)?>
        });
    });
</script>
<?php 
endif;

function push_dynatree_node_recur(&$dynatree_node, $tree_nodes) {
	foreach ($tree_nodes as $node) {
		$d_node = array('title' => $node['data']['name'], 'key' => $node['data']['id'], 'select' => ($node['data']['select'] ? true : false), 'expand' => ($node['data']['expand'] ? true : false));
		if (isset($node['children']) && !empty($node['children'])) {
			$d_node['isFolder'] = 'true';
			$d_node['children'] = array();
			push_dynatree_node_recur($d_node['children'], $node['children']);
		}
		$dynatree_node[] = $d_node;
	}
}
function cats_2_options_recur($tree_nodes, $indent = '') {
	foreach ($tree_nodes as $node) {
		echo '<option value="' . $node['data']['id'] . '">' . $indent . $node['data']['name'] . '</option>';
		if (isset($node['children']) && !empty($node['children'])) {
			cats_2_options_recur($node['children'], $indent . '&nbsp;&nbsp;&nbsp;&nbsp;');
		}
	}
}
?>